﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;

using Stock.Model;
using Stock.Service;

namespace Stock.ViewModel
{
    public class StockDetailViewModel : ViewModelBase
    {
        private EasyMoneyService easyMoneyService;

        public StockDetailViewModel()
        {
            Init();
        }

        private string _code;

        public string Code
        {
            get { return _code; }
            set
            {
                Set(ref _code, value);
                InitData();
            }
        }

        private FundModel _fund;

        public FundModel Fund { get => _fund; set => Set(ref _fund, value); }

        private List<FundMemberModel> _fundMemberList;

        public List<FundMemberModel> FundMemberList { get => _fundMemberList; set => Set(ref _fundMemberList, value); }

        private List<FundJZModel> _fundJZList;

        public List<FundJZModel> FundJZList { get => _fundJZList; set => Set(ref _fundJZList, value); }

        private int _pageIndex;
        private int _pageSize;
        private int _totalCount;
        private int _totalPage;

        private string _currentPageString;

        public int PageIndex
        {
            get => _pageIndex;
            set
            {
                Set(ref _pageIndex, value);
                InitJZData();
            }
        }

        public int TotalCount { get => _totalCount; set => Set(ref _totalCount, value); }

        public int TotalPage { get => _totalPage; set => Set(ref _totalPage, value); }

        public string CurrentPageString { get => _currentPageString; set => Set(ref _currentPageString, value); }

        public RelayCommand FirstPageCmd => new RelayCommand(FirstPage);

        public RelayCommand LastPageCmd => new RelayCommand(LastPage);

        public RelayCommand PrePageCmd => new RelayCommand(PrePage);

        public RelayCommand NextPageCmd => new RelayCommand(NextPage);
        private void Init()
        {
            _pageIndex = 1;
            _pageSize = 10;
            easyMoneyService = SimpleIoc.Default.GetInstance<EasyMoneyService>();
        }

        private async void InitData()
        {
            Fund = await easyMoneyService.GetFundModel(_code);
            FundMemberList = await easyMoneyService.GetFundMemberList(_code);
        }

        private async void InitJZData()
        {
            var model = await easyMoneyService.GetFundJZModel(_pageIndex, _pageSize, _code);
            if (model != null)
            {
                FundJZList = model.List;
                TotalCount = model.TotalCount;

                TotalPage = (_totalCount + _pageSize - 1) / _pageSize;

                CurrentPageString = $"{_pageIndex} / {_totalPage}";
            }
        }

        private void FirstPage()
        {
            if (_pageIndex > 1)
            {
                PageIndex = 1;
            }
        }

        private void LastPage()
        {
            if (_pageIndex < _totalPage)
            {
                PageIndex = _totalPage;
            }
        }

        private void PrePage()
        {
            if (_pageIndex > 1)
            {
                PageIndex--;
            }
        }

        private void NextPage()
        {
            if (_pageIndex < _totalPage)
            {
                PageIndex++;
            }
        }
    }
}
